package manager.cms.service.exec;

import common.SnowflakeIdWorker;
import manager.cms.dao.CmsContentCatDao;
import manager.cms.dao.CmsContentDao;
import manager.cms.service.JdExecutor;
import manager.cms.vo.CmsContentCatVO;
import manager.cms.vo.CmsContentVO;
import manager.feign.ShopIndexFloorThirdService;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 获取优惠卷信息
 */
@Service(value = "newArrivalExec")
public class NewArrivalExecImpl implements JdExecutor {
    @Autowired
    private ShopIndexFloorThirdService shopIndexFloorThirdService;
    @Autowired
    private CmsContentCatDao cmsContentCatDao;
    @Autowired
    private CmsContentDao cmsContenDao;
    @Override
    public void execute(Map<String,Object> mapParams) {
        try {
            JSONObject jsonObject = null;
            CmsContentCatVO cmsContentCatVO=new CmsContentCatVO();
            String  coupon=shopIndexFloorThirdService.getNewArrival();
            String haoDianDataStr=coupon.substring(16,coupon.length()-1);
            jsonObject = new JSONObject(haoDianDataStr);
            JSONArray haoDianData=(JSONArray)jsonObject.get("data");
            cmsContentCatVO.setName("新品首发");
            String haoDianDataId=String.valueOf(SnowflakeIdWorker.nextId());
            cmsContentCatVO.setId(haoDianDataId);
            cmsContentCatDao.addCmsContentCat(cmsContentCatVO);
            List<CmsContentVO> contentVOList=new ArrayList<CmsContentVO>();
            for(int i=0;i<haoDianData.length();i++){
                CmsContentVO cmsContentVO=new CmsContentVO();
                String id=String.valueOf(SnowflakeIdWorker.nextId());
                JSONObject item=(JSONObject)haoDianData.get(i);
                cmsContentVO.setId(id);
                cmsContentVO.setPic(item.get("img").toString());
                cmsContentVO.setTitle(item.get("name").toString());
                cmsContentVO.setCategoryId(haoDianDataId);
                contentVOList.add(cmsContentVO);
            }
            cmsContenDao.addCmsContentBatch(contentVOList);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
